package com.simple.puzzle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class HighScore extends Activity implements OnClickListener {

	ListView listview;

	ListHighScoreAdapter adapter;
	ArrayList<Score> scores = new ArrayList<Score>();

	TextView bar_name;
	TextView bar_time;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.high_score);

		listview = (ListView) findViewById(R.id.high_score_list);

		ScoreList sList = new ScoreList();
		scores = sList.scores;

		//
		sortWithTime();

		bar_name = (TextView) findViewById(R.id.bar_name);
		bar_time = (TextView) findViewById(R.id.bar_time);

		bar_name.setOnClickListener(this);
		bar_time.setOnClickListener(this);
	}

	/**
	 * sort list
	 */
	public void sortWithTime() {
		Comparator<Score> comparator = new Comparator<Score>() {
			@Override
			public int compare(Score object1, Score object2) {
				// TODO Auto-generated method stub
				int time1 = Integer.valueOf(object1.time);
				int time2 = Integer.valueOf(object2.time);
				if (time1 > time2)
					return 1;
				else if (time1 < time2)
					return -1;
				else
					return 0;
			}
		};
		Collections.sort(scores, comparator);

		adapter = new ListHighScoreAdapter(this, R.id.high_score_list, scores);
		listview.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == bar_name) {
			Comparator<Score> comparator = new Comparator<Score>() {
				@Override
				public int compare(Score object1, Score object2) {
					// TODO Auto-generated method stub
					return object1.name.compareToIgnoreCase(object2.name);
				}
			};
			Collections.sort(scores, comparator);

			adapter = new ListHighScoreAdapter(this, R.id.high_score_list,
					scores);
			listview.setAdapter(adapter);
		} else if (v == bar_time) {
			sortWithTime();
		}
	}
}
